{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting python-pptx\n",
      "  Downloading python-pptx-0.6.21.tar.gz (10.1 MB)\n",
      "Requirement already satisfied: lxml>=3.1.0 in d:\\2345downloads\\lib\\site-packages (from python-pptx) (4.6.3)\n",
      "Requirement already satisfied: Pillow>=3.3.2 in d:\\2345downloads\\lib\\site-packages (from python-pptx) (8.4.0)\n",
      "Requirement already satisfied: XlsxWriter>=0.5.7 in d:\\2345downloads\\lib\\site-packages (from python-pptx) (3.0.1)\n",
      "Building wheels for collected packages: python-pptx\n",
      "  Building wheel for python-pptx (setup.py): started\n",
      "  Building wheel for python-pptx (setup.py): finished with status 'done'\n",
      "  Created wheel for python-pptx: filename=python_pptx-0.6.21-py3-none-any.whl size=470950 sha256=1d1fce89488291c2f53c4518296c7aa7ce5708929cef2afb2f49478a2ec5b602\n",
      "  Stored in directory: c:\\users\\未来可期\\appdata\\local\\pip\\cache\\wheels\\0e\\4a\\ed\\9653bc799915f52dce3f04d14946fbd85cce9c3cdedc9cfa71\n",
      "Successfully built python-pptx\n",
      "Installing collected packages: python-pptx\n",
      "Successfully installed python-pptx-0.6.21\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install python-pptx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: snownlp in d:\\2345downloads\\lib\\site-packages (0.12.3)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install snownlp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "4 reasons you should take a nap\n",
      "\n",
      "4 reasons you should take a nap\n",
      "\n",
      "https://language.chinadaily.com.cn/a/202204/12/WS6254da6da310fd2b29e5668a.html\n",
      "https://language.chinadaily.com.cn/a/202204/12/WS6254da6da310fd2b29e5668a.html\n",
      "01\n",
      "01\n",
      "目录\n",
      "目录\n",
      "CONTENTS\n",
      "CONTENTS\n",
      "02\n",
      "02\n",
      "03\n",
      "03\n",
      "04\n",
      "04\n",
      "summary\n",
      "summary\n",
      "words\n",
      "words\n",
      "translation\n",
      "translation\n",
      "Small tips\n",
      "Small tips\n",
      "01\n",
      "01\n",
      "Summary\n",
      "Summary\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "This article is an expository text, it introduces four benefits of having a nap at noon. They are charging your brain’s batteries, lowering risk for heart problems, helping to get into shape and boosting creativity. It has a clear structure and it’s easy for us to understand.\n",
      "This article is an expository text, it introduces four benefits of having a nap at noon. They are charging your brain’s batteries, lowering risk for heart problems, helping to get into shape and boosting creativity. It has a clear structure and it’s easy for us to understand.\n",
      "02\n",
      "02\n",
      ".\n",
      ".\n",
      "Word\n",
      "Word\n",
      "02\n",
      "02\n",
      "to satisfy a particular desire, interest, etc.\n",
      "The inheritance enabled him to indulge his passion for art.\n",
      "这笔遗产使他能够尽情投入他热爱的艺术。\n",
      "\n",
      "to satisfy a particular desire, interest, etc.\n",
      "The inheritance enabled him to indulge his passion for art.\n",
      "这笔遗产使他能够尽情投入他热爱的艺术。\n",
      "\n",
      "\n",
      "\n",
      "a person or thing that has the same position or function as sb/sth else in a different place or situation\n",
      "a person or thing that has the same position or function as sb/sth else in a different place or situation\n",
      "\n",
      "\n",
      "A short snooze may also make you feel more relaxed..\n",
      "A short snooze may also make you feel more relaxed..\n",
      "Snooze: verb\n",
      "to have a short light sleep, especially during the day and usually not in bed\n",
      "\n",
      "Snooze: verb\n",
      "to have a short light sleep, especially during the day and usually not in bed\n",
      "\n",
      "I often have a snooze after lunch.\n",
      "我常在午饭后睡个小觉。\n",
      "I often have a snooze after lunch.\n",
      "我常在午饭后睡个小觉。\n",
      "Indulge:\n",
      "Indulge:\n",
      "the researchers found that those who indulged in occasional napping\n",
      "the researchers found that those who indulged in occasional napping\n",
      "counterpart\n",
      "counterpart\n",
      "Researchers monitoring the brain activity of 15 people found that the right side of their brain communicated busily with itself as well as with its left counterpart.\n",
      "Researchers monitoring the brain activity of 15 people found that the right side of their brain communicated busily with itself as well as with its left counterpart.\n",
      "The Foreign Minister held talks with his Chinese counterpart.\n",
      "外交部长与中国外交部长举行了会谈。\n",
      "\n",
      "The Foreign Minister held talks with his Chinese counterpart.\n",
      "外交部长与中国外交部长举行了会谈。\n",
      "\n",
      "03\n",
      "03\n",
      "translation\n",
      "translation\n",
      "\n",
      "\n",
      "02\n",
      "02\n",
      "\n",
      "\n",
      "The right side of your brain might experience a mental spark during a nap, research has suggested. The right side is the hemisphere most associated with creative tasks, such as visualization and thinking, while the left is more analytic.\n",
      "The right side of your brain might experience a mental spark during a nap, research has suggested. The right side is the hemisphere most associated with creative tasks, such as visualization and thinking, while the left is more analytic.\n",
      "研究表明，小憩期间，右脑可能迸发灵感。右脑大多和创造性活动有关，比如视觉活动和思考。左脑更倾向于分析活动。\n",
      "研究表明，小憩期间，右脑可能迸发灵感。右脑大多和创造性活动有关，比如视觉活动和思考。左脑更倾向于分析活动。\n",
      "研究表明，小憩时右脑可能会产生灵感。右脑与想象、思考等创造性活动相关，而左脑则更善于分析。\n",
      "研究表明，小憩时右脑可能会产生灵感。右脑与想象、思考等创造性活动相关，而左脑则更善于分析。\n",
      "研究表明，在午睡时，你的右脑可能会出现精神火花。右侧是与创造性任务（如可视化和思考）最相关的半球，而左侧则更具分析性。\n",
      "研究表明，在午睡时，你的右脑可能会出现精神火花。右侧是与创造性任务（如可视化和思考）最相关的半球，而左侧则更具分析性。\n",
      "04\n",
      "04\n",
      "Small tips\n",
      "Small tips\n",
      "Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.\n",
      "Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.Click here to enter your text.\n",
      "02\n",
      "02\n",
      "The nap time should be controlled within half an hour, otherwise it will be uncomfortable to wake up.\n",
      "The nap time should be controlled within half an hour, otherwise it will be uncomfortable to wake up.\n",
      "\n",
      "\n",
      "Although taking a nap is just taking a nap, you must not be too casual. Don't sit or lie on the table.\n",
      "Although taking a nap is just taking a nap, you must not be too casual. Don't sit or lie on the table.\n",
      "\n",
      "\n",
      "When the weather is hot, you should also cover your abdomen with a towel or quilt during nap to prevent the cool air from taking advantage of the weakness.\n",
      "When the weather is hot, you should also cover your abdomen with a towel or quilt during nap to prevent the cool air from taking advantage of the weakness.\n",
      "\n",
      "\n",
      "https://foxueol.com/article/33420\n",
      "https://foxueol.com/article/33420\n",
      "strike a proper balance between work and rest\n",
      "strike a proper balance between work and rest\n",
      "02\n",
      "02\n",
      "That’s all! Thanks for listening!\n",
      "That’s all! Thanks for listening!\n"
     ]
    }
   ],
   "source": [
    "#coding:utf-8\n",
    "from pptx import Presentation\n",
    "\n",
    "prs = Presentation(r\"D:\\360极速浏览器下载\\luningyingyu\\news sharing2.0.pptx\")\n",
    "for slide in prs.slides:\n",
    "    for shape in slide.shapes:\n",
    "      if shape.has_text_frame:      \n",
    "          text_frame = shape.text_frame\n",
    "          print(text_frame.text)\n",
    "          for paragraph in text_frame.paragraphs:\n",
    "              print(paragraph.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from pptx import Presentation\n",
    "from docx import Document\n",
    "\n",
    "doc = Document()\n",
    "prs = Presentation(r\"D:\\360极速浏览器下载\\luningyingyu\\news sharing2.0.pptx\")\n",
    "ls = []\n",
    "for slide in prs.slides:\n",
    "\tfor shape in slide.shapes:\n",
    "\t\tif shape.has_text_frame:\n",
    "\t\t\ttext_frame = shape.text_frame\n",
    "\t\t\tfor paragraph in text_frame.paragraphs:\n",
    "\t\t\t\tif paragraph.text != '':\n",
    "\t\t\t\t\tdoc.add_paragraph(paragraph.text)\n",
    "\n",
    "doc.save(\"news sharing2.0.docx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "from pptx import Presentation\n",
    "\n",
    "# 新建演示文稿\n",
    "prs = Presentation()\n",
    "\n",
    "# 获取幻灯片布局\n",
    "title_slide_layout = prs.slide_layouts[0]\n",
    "\n",
    "# 基于布局新增一张幻灯片\n",
    "slide = prs.slides.add_slide(title_slide_layout)\n",
    "\n",
    "# 获取标题占位符\n",
    "title = slide.shapes.title\n",
    "# 副标题占位符\n",
    "subtitle = slide.placeholders[1]\n",
    "\n",
    "# 设置标题和副标题\n",
    "title.text = \"Hello, World!\"\n",
    "subtitle.text = \"python-pptx was here!\"\n",
    "\n",
    "# 保存演示文稿\n",
    "prs.save('522_1.pptx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pptx import Presentation\n",
    "# 新建演示文稿\n",
    "prs = Presentation()\n",
    "# 获取幻灯片布局\n",
    "bullet_slide_layout = prs.slide_layouts[1]\n",
    "\n",
    "#增加幻灯片\n",
    "slide = prs.slides.add_slide(bullet_slide_layout)\n",
    "#获取形状对象\n",
    "shapes = slide.shapes\n",
    "#获取标题占位符形状\n",
    "title_shape = shapes.title\n",
    "\n",
    "#获取页面主体的占位符\n",
    "body_shape = shapes.placeholders[1]\n",
    "#幻灯片标题\n",
    "title_shape.text = 'Adding a Bullet Slide'\n",
    "# 文本框\n",
    "tf = body_shape.text_frame\n",
    "#文本框内容\n",
    "tf.text = 'Find the bullet slide layout'\n",
    "\n",
    "# 文本框增加段落\n",
    "p = tf.add_paragraph()\n",
    "# 段落内容\n",
    "p.text = 'Use _TextFrame.text for first bullet'\n",
    "# 缩进级别（范围0-8）\n",
    "p.level = 1\n",
    "\n",
    "p = tf.add_paragraph()\n",
    "p.text = 'Use _TextFrame.add_paragraph() for subsequent bullets'\n",
    "p.level = 2\n",
    "# 保存演示文稿\n",
    "prs.save('522_2.pptx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pptx import Presentation\n",
    "from pptx.enum.shapes import MSO_SHAPE  \n",
    "from pptx.util import Inches, Pt # 尺寸\n",
    "from pptx.enum.dml import MSO_THEME_COLOR \n",
    "\n",
    "prs = Presentation()\n",
    "slide_layout = prs.slide_layouts[0]\n",
    "slide = prs.slides.add_slide(slide_layout)\n",
    "shapes = slide.shapes\n",
    "\n",
    "# 遍历文本框\n",
    "for shape in slide.shapes:\n",
    "    if not shape.has_text_frame:\n",
    "        continue\n",
    "    text_frame = shape.text_frame\n",
    "# 准备三个段落的内容，此处为一个3个元素的数组\n",
    "paragraph_strs = [\n",
    "    'Egg, bacon, sausage and spam.',\n",
    "    'Spam, bacon, sausage and spam.',\n",
    "    'Spam, egg, spam, spam, bacon and spam.'\n",
    "]\n",
    "# 获取文本框\n",
    "\n",
    "#清空文本框中可能存在的任何内容\n",
    "text_frame.clear()\n",
    "#增加一个段落\n",
    "p = text_frame.paragraphs[0]\n",
    "#填充段落内容\n",
    "p.text = paragraph_strs[0]\n",
    "#追加后面两个段落\n",
    "for para_str in paragraph_strs[1:]:\n",
    "    p = text_frame.add_paragraph()\n",
    "    p.text = para_str\n",
    "#一种更快捷的方法将文字插入到段落中\n",
    "shape.text = 'foobar'\n",
    "prs.save('522_3.pptx')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#找自己电脑上的.ppt后缀的PPT演示文档，调研相关的库和依赖，进行读写处理\n",
    "import win32com\n",
    "from win32com.client import Dispatch\n",
    "import os\n",
    "\n",
    "ppt = Dispatch('PowerPoint.Application')\n",
    "# 或者使用下面的方法，使用启动独立的进程：\n",
    "# ppt = DispatchEx('PowerPoint.Application')\n",
    "\n",
    "# 如果不声明以下属性，运行的时候会显示的打开word\n",
    "ppt.Visible = 1  # 后台运行\n",
    "ppt.DisplayAlerts = 0  # 不显示，不警告\n",
    "\n",
    "# 创建新的PowerPoint文档\n",
    "# pptSel = ppt.Presentations.Add() \n",
    "# 打开一个已有的PowerPoint文档\n",
    "pptSel = ppt.Presentations.Open(os.path.join(os.getcwd(),r\"D:\\未来可期\\ppttest.ppt\"))\n",
    "\n",
    "# 复制模板页\n",
    "pptSel.Slides(1).Copy()\n",
    "#设置需要复制的模板页数\n",
    "pageNums = 10\n",
    "# 粘贴模板页\n",
    "for i in range(pageNums):\n",
    "    pptSel.Slides.Paste()\n",
    "\n",
    "# pptSel.Save()  # 保存\n",
    "pptSel.SaveAs(os.path.join(os.getcwd(),\"win32_copy模板.pptx\"))  # 另存为\n",
    "pptSel.Close()  # 关闭 PowerPoint 文档\n",
    "ppt.Quit()  # 关闭 office"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#调研并练习PDF（文字版）与WORD文档的相互转换。\n",
    "from win32com import client as wc\n",
    "\n",
    "wordhandle = wc.Dispatch(\"Word.Application\")\n",
    "wordhandle.Visible = 0 # 后台运行，不显示\n",
    "wordhandle.DisplayAlerts = 0  #不警告\n",
    "doc = wordhandle.Documents.Open(r'D:\\未来可期\\智能教学系统.docx')\n",
    "doc.SaveAs('D:\\未来可期\\智能教学系统修改版.pdf', 17) #  txt=4, html=10, docx=16， pdf=17\n",
    "doc.Close()\n",
    "#docx.Quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "NotADirectoryError",
     "evalue": "[WinError 267] 目录名称无效。: 'D:\\\\未来可期\\\\共同富裕.pdf'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNotADirectoryError\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32mD:\\temp/ipykernel_14112/3261637460.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetcwd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mchdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr'D:\\未来可期\\共同富裕.pdf'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mreportlab\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpdfgen\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mcanvas\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mreportlab\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0munits\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mcm\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNotADirectoryError\u001b[0m: [WinError 267] 目录名称无效。: 'D:\\\\未来可期\\\\共同富裕.pdf'"
     ]
    }
   ],
   "source": [
    "#调研并练习PDF加密与水印\n",
    "import os\n",
    "os.getcwd() \n",
    "os.chdir(r'D:\\未来可期\\共同富裕.pdf') \n",
    "from reportlab.pdfgen import canvas\n",
    "from reportlab.lib.units import cm\n",
    "from reportlab.pdfbase import pdfmetrics \n",
    "from reportlab.pdfbase.ttfonts import TTFont \n",
    "pdfmetrics.registerFont(TTFont('song', 'C:/Windows/Fonts/simsun.ttc'))#宋体\n",
    "from PyPDF2 import PdfFileWriter,PdfFileReader\n",
    "import xlrd\n",
    "\n",
    "def create_watermark(content):\n",
    "    #默认大小为21cm*29.7cm\n",
    "    c = canvas.Canvas('mark.pdf', pagesize = (30*cm, 30*cm))   \n",
    "    c.translate(10*cm, 10*cm) #移动坐标原点(坐标系左下为(0,0)))                                                                                                                             \n",
    "    c.setFont('song',22)#设置字体为宋体，大小22号\n",
    "    c.setFillColorRGB(0.5,0.5,0.5)#灰色                                                                                                                         \n",
    "    c.rotate(45)#旋转45度，坐标系被旋转\n",
    "    c.drawString(-7*cm, 0*cm, content)\n",
    "    c.drawString(7*cm, 0*cm, content)\n",
    "    c.drawString(0*cm, 7*cm, content)\n",
    "    c.drawString(0*cm, -7*cm, content)                                                                                                                              \n",
    "    c.save()#关闭并保存pdf文件 \n",
    "from reportlab.pdfbase.ttfonts import TTFont\n",
    "c.setFont('song',22)#设置字体为宋体，大小22号\n",
    "def add_watermark2pdf(input_pdf,output_pdf,watermark_pdf):\n",
    "    watermark = PdfFileReader(watermark_pdf)\n",
    "    watermark_page = watermark.getPage(0)\n",
    "    pdf = PdfFileReader(input_pdf,strict=False)\n",
    "    pdf_writer = PdfFileWriter()\n",
    "    for page in range(pdf.getNumPages()):\n",
    "        pdf_page = pdf.getPage(page)\n",
    "        pdf_page.mergePage(watermark_page)\n",
    "        pdf_writer.addPage(pdf_page)\n",
    "    pdfOutputFile = open(output_pdf,'wb')   \n",
    "    pdf_writer.encrypt('scb2018')#设置pdf密码\n",
    "    pdf_writer.write(pdfOutputFile)\n",
    "    pdfOutputFile.close()\n",
    "   \n",
    "    ExcelFile = xlrd.open_workbook('D:\\电子知识库\\北京语言大学2021-2022学年第二学期慕课及共享通识课汇总表.xlsx')\n",
    "sheet=ExcelFile.sheet_by_name('Sheet2')#打开有商家名单那个sheet\n",
    "print('———————已导入商家名单———————')\n",
    "col = sheet.col_values(3)#第4列内容为商家名称\n",
    "id = sheet.col_values(0)#第1列内容为ID\n",
    "del col[0];del id[0]#去掉标题\n",
    "id2 = [str(int(i)) for i in id]\n",
    "merchant_as_mark_content =[(i+'  ')*4 if len(i)<=5 else i for i in col]#如果名称太短则重复4个为一行\n",
    "sheet.col_values(3)\n",
    "if __name__=='__main__':\n",
    "    for i,j,k in zip(merchant_as_mark_content,id2):#i制作水印，j文件名，k对应ID\n",
    "        create_watermark(i)#创造了一个水印pdf：mark.pdf\n",
    "        add_watermark2pdf('需要加水印的源文件.pdf',k+'通知('+j+').pdf','mark.pdf')\n",
    "        print('———————已制作好第'+k+'个pdf，正在准备下一个———————')\n",
    "    print('———————所有文件已转化完毕———————')   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "com_error",
     "evalue": "(-2147352567, '发生意外。', (0, 'Microsoft Word', '很抱歉，找不到您的文件。该项目是否已移动、重命名或删除?\\r (C:\\\\Users\\\\未来可期\\\\Documents\\\\test_1.docx)', 'wdmain11.chm', 24654, -2146823114), None)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mcom_error\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mD:\\temp/ipykernel_14112/146790810.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mwordhandle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mVisible\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;31m# 后台运行，不显示\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mwordhandle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDisplayAlerts\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m  \u001b[1;31m#不警告\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mdoc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mwordhandle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDocuments\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mOpen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'test_1.docx'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[0mdoc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSaveAs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'hhh.pdf'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m17\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#  txt=4, html=10, docx=16， pdf=17\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[0mdoc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mClose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\2345Downloads\\lib\\site-packages\\win32com\\client\\dynamic.py\u001b[0m in \u001b[0;36mOpen\u001b[1;34m(self, FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenAndRepair, DocumentDirection, NoEncodingDialog, XMLTransform)\u001b[0m\n",
      "\u001b[1;31mcom_error\u001b[0m: (-2147352567, '发生意外。', (0, 'Microsoft Word', '很抱歉，找不到您的文件。该项目是否已移动、重命名或删除?\\r (C:\\\\Users\\\\未来可期\\\\Documents\\\\test_1.docx)', 'wdmain11.chm', 24654, -2146823114), None)"
     ]
    }
   ],
   "source": [
    "\n",
    "from win32com import client as wc\n",
    "\n",
    "wordhandle = wc.Dispatch(\"Word.Application\")\n",
    "wordhandle.Visible = 0 # 后台运行，不显示\n",
    "wordhandle.DisplayAlerts = 0  #不警告\n",
    "doc = wordhandle.Documents.Open('test_1.docx')\n",
    "doc.SaveAs('hhh.pdf', 17) #  txt=4, html=10, docx=16， pdf=17\n",
    "doc.Close()\n",
    "#准备若干类别Office文档，doc\\docx\\xls\\xlsx\\pdf等都要有。学习并实验通过Win32 API调用MS Office应用程序和WPS Office应用程序后台，操作Office文档与PDF文档的转换。\n",
    "#调研并练习PDF文档中表格的提取,并用python将提取的表格内容保存到Excel。调研并练习PDF文档中图片的提取和保存。调研并练习PDF文档转图片、图片合成PDF文档。'''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: PyPDF2 in d:\\2345downloads\\lib\\site-packages (1.27.12)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install PyPDF2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pdfplumber\n",
      "  Downloading pdfplumber-0.6.2-py3-none-any.whl (36 kB)\n",
      "Collecting pdfminer.six==20220319\n",
      "  Downloading pdfminer.six-20220319-py3-none-any.whl (5.6 MB)\n",
      "Collecting Wand>=0.6.7\n",
      "  Downloading Wand-0.6.7-py2.py3-none-any.whl (139 kB)\n",
      "Collecting Pillow>=9.1\n",
      "  Downloading Pillow-9.1.1-cp39-cp39-win_amd64.whl (3.3 MB)\n",
      "Requirement already satisfied: cryptography in d:\\2345downloads\\lib\\site-packages (from pdfminer.six==20220319->pdfplumber) (3.4.8)\n",
      "Requirement already satisfied: chardet in d:\\2345downloads\\lib\\site-packages (from pdfminer.six==20220319->pdfplumber) (4.0.0)\n",
      "Requirement already satisfied: cffi>=1.12 in d:\\2345downloads\\lib\\site-packages (from cryptography->pdfminer.six==20220319->pdfplumber) (1.14.6)\n",
      "Requirement already satisfied: pycparser in d:\\2345downloads\\lib\\site-packages (from cffi>=1.12->cryptography->pdfminer.six==20220319->pdfplumber) (2.20)\n",
      "Installing collected packages: Wand, Pillow, pdfminer.six, pdfplumber\n",
      "  Attempting uninstall: Pillow\n",
      "    Found existing installation: Pillow 8.4.0\n",
      "    Uninstalling Pillow-8.4.0:\n",
      "      Successfully uninstalled Pillow-8.4.0\n",
      "Successfully installed Pillow-9.1.1 Wand-0.6.7 pdfminer.six-20220319 pdfplumber-0.6.2\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install pdfplumber"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "北京语言大学2021-2022学年第二学期通识课时间地点表\n",
      "课程号 课程名 上课教师 课容量 总学时 学分 时间 上课地点 上课周次\n",
      "213013022T 中国文化遗产选讲 贾蕾 90 33 2 星期三9-11节 教一501 1-10,12周上\n",
      "213013152T 国际关系视角下的国际法 孙迎春 90 33 2 星期三9-11节 主南102 1-10,12周上\n",
      "213013782T 中国经典书画品鉴 尹成君等 90 33 2 星期三9-11节 主北158 1-9,11-12周上\n",
      "213013792T 心理学与生活 江新等 90 33 2 星期三9-11节 主南200 1-9,11-12周上\n",
      "213210292T 现代外语教学法 许宏晨 60 33 2 星期三9-11节 教五110 1-9,11-12周上\n",
      "213210332T 北京话的前世和今生 黄晓东 60 33 2 星期三9-11节 教五123 1-9,11-12周上\n",
      "213210372T 在线教育设计与实践 郑艳群 60 33 2 星期三9-11节 主南116 1-9,11-12周上\n",
      "213210462T 哲学与理性 周菲菲 60 33 2 星期三9-11节 主南201 1-9,11-12周上\n",
      "213210482T 语言魅力与研究漫谈 陈香兰 60 33 2 星期三9-11节 主南203 1-9,11-12周上\n",
      "213210502T 唐代女性与文学十讲 刘淑丽 60 33 2 星期三9-11节 教二104 1-9,11-12周上\n",
      "213210522T 中国古代文学经典赏读 周阅 60 33 2 星期三9-11节 教二105 1-9,11-12周上\n",
      "213210542T 论语精读 方铭 60 33 2 星期三9-11节 教二107 1-9,11-12周上\n",
      "操场体育馆\n",
      "213210252T 跆拳道 韩鹏 30 34 2 星期四1-2节 1-17周\n",
      "204\n",
      "213210562T 网球 李可 30 34 2 星期四1-2节 网球馆 1-17周\n",
      "213012132T 语言+ 司富珍等 60 33 2 星期四9-11节 教五123 1-11周上\n",
      "213012182T 聆听国乐 凌博,王宁 90 33 2 星期四9-11节 主北158 1-11周上\n",
      "213012762T 国别经济与发展概论 徐云松 90 33 2 星期四9-11节 教一601 1-11周上\n",
      "213013042T 婚姻家庭法 李明奇 90 33 2 星期四9-11节 主南102 1-11周上\n",
      "213013252T 丝绸之路与中西交流 王丽娜 90 33 2 星期四9-11节 教一401 1-11周上\n",
      "213013492T 大数据与可视化 李吉梅,赵慧周 60 33 2 星期四9-11节 教一108 1-11周上\n",
      "213210272T 国别和区域研究导论 邵玉琢 60 33 2 星期四9-11节 教二105 1-11周上\n",
      "213210402T 宏观经济学通识 陈胤默 60 33 2 星期四9-11节 教五110 1-11周上\n",
      "213210602T 大学语文 梁彦民等 60 33 2 星期四9-11节 主南116 1-11周上\n",
      "213210472T 汉字认知与文化 杨群 60 33 2 星期四9-11节 教二107 1-11周上\n",
      "西方人眼中的中国——汉学\n",
      "213210532T 张道建 60 33 2 星期四9-11节 教二104 1-11周上\n",
      "研究与文明对话\n",
      "213210552T 朋辈心理助人技能训练 刘梦曦等 20 33 2 星期四9-11节 教一819 1-11周上\n",
      "213210582T “一带一路”沿线国家简史 王义夫 90 33 2 星期四9-11节 教一701 1-11周上\n",
      "213210232T 中国作家公开课 徐宝锋 90 33 2 星期五5-7节 教二131 1-11周上\n",
      "1-4,6-7,10-13,15周\n",
      "213013032T 《聊斋志异》赏析 成敏 50 33 2 星期日5-7节 教二237\n",
      "上\n",
      "英语运用能力训练与提高 1-4,6-7,10-13,15周\n",
      "213011982T 王冰欣等 50 33 2 星期日5-7节 主南102\n",
      "（雅思） 上\n",
      "213210591M 生态文明（慕课） 智慧树 30 17 1\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import pdfplumber\n",
    "\n",
    "def extract_text_info(filepath):\n",
    "    \"\"\"\n",
    "    提取PDF中的文字\n",
    "    @param filepath:文件路径\n",
    "    @return:\n",
    "    \"\"\"\n",
    "    with pdfplumber.open(filepath) as pdf:\n",
    "        # 获取第2页数据\n",
    "        page = pdf.pages[1]\n",
    "        print(page.extract_text())\n",
    "        \n",
    "# 提取文字内容\n",
    "extract_text_info(r\"D:\\电子知识库\\北京语言大学2021-2022学年第二学期通识课时间地点表.pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)1.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)2.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)3.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)4.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)5.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)6.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)7.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)8.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)9.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)10.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)11.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)12.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)13.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)14.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)15.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)16.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)17.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)18.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)19.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)20.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)21.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)22.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)23.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)24.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)25.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)26.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)27.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)28.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)29.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)30.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)31.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)32.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)33.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)34.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)35.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)36.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)37.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)38.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)39.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)40.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)41.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)42.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)43.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)44.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)45.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)46.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)47.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)48.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)49.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)50.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)51.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)52.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)53.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)54.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)55.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)56.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)57.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)58.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)59.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)60.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)61.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)62.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)63.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)64.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)65.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)66.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)67.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)68.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)69.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)70.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)71.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)72.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)73.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)74.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)75.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)76.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)77.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)78.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)79.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)80.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)81.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)82.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)83.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)84.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)85.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)86.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)87.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)88.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)89.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)90.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)91.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)92.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)93.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)94.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)95.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)96.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)97.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)98.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)99.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)100.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)101.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)102.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)103.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)104.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)105.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)106.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)107.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)108.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)109.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)110.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)111.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)112.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)113.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)114.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)115.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)116.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)117.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)118.pdf\n",
      "D:\\电子知识库\\Vescode\\python2.0\\傲慢与偏见(英文原版)119.pdf\n",
      "文件已成功拆分，保存路径为：D:\\电子知识库\\Vescode\\python2.0\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from PyPDF2 import PdfFileWriter, PdfFileReader\n",
    "def split_pdf(filename, filepath, save_dirpath, step=5):\n",
    "    \"\"\"\n",
    "    拆分PDF为多个小的PDF文件，\n",
    "    @param filename:文件名\n",
    "    @param filepath:文件路径\n",
    "    @param save_dirpath:保存小的PDF的文件路径\n",
    "    @param step: 每step间隔的页面生成一个文件，例如step=5，表示0-4页、5-9页...为一个文件\n",
    "    @return:\n",
    "    \"\"\"\n",
    "    if not os.path.exists(save_dirpath):\n",
    "        os.mkdir(save_dirpath)\n",
    "    pdf_reader = PdfFileReader(filepath)\n",
    "    # 读取每一页的数据\n",
    "    pages = pdf_reader.getNumPages()\n",
    "    for page in range(0, pages, step):\n",
    "        pdf_writer = PdfFileWriter()\n",
    "        # 拆分pdf，每 step 页的拆分为一个文件\n",
    "        for index in range(page, page+step):\n",
    "            if index < pages:\n",
    "                pdf_writer.addPage(pdf_reader.getPage(index))\n",
    "        # 保存拆分后的小文件\n",
    "        save_path = os.path.join(save_dirpath, filename+str(int(page/step)+1)+'.pdf')\n",
    "        print(save_path)\n",
    "        with open(save_path, \"wb\") as out:\n",
    "            pdf_writer.write(out)\n",
    "\n",
    "    print(\"文件已成功拆分，保存路径为：\"+save_dirpath)\n",
    "    \n",
    "\n",
    "   \n",
    "    \n",
    "   \n",
    "    \n",
    "split_pdf(\"傲慢与偏见(英文原版)\",r\"D:\\电子知识库\\电子书\\傲慢与偏见(英文原版).pdf\",r\"D:\\电子知识库\\Vescode\\python2.0\", step=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from PyPDF2 import PdfFileReader, PdfFileWriter\n",
    "\n",
    "def concat_pdf(filename, read_dirpath, save_filepath):\n",
    "    \"\"\"\n",
    "    合并多个PDF文件\n",
    "    @param filename:文件名\n",
    "    @param read_dirpath:要合并的PDF目录\n",
    "    @param save_filepath:合并后的PDF文件路径\n",
    "    @return:\n",
    "    \"\"\"\n",
    "    pdf_writer = PdfFileWriter()\n",
    "    # 对文件名进行排序\n",
    "    list_filename = os.listdir(r\"D:\\电子知识库\\Vescode\\python2.0\")\n",
    "    list_filename.sort(key=lambda x: int(x[:-4].replace(filename, \"\")))\n",
    "    for filename in list_filename:\n",
    "        print(filename)\n",
    "        filepath = os.path.join(read_dirpath, filename)\n",
    "        # 读取文件并获取文件的页数\n",
    "        pdf_reader = PdfFileReader()\n",
    "        pages = pdf_reader.getNumPages()\n",
    "        # 逐页添加\n",
    "        for page in range(pages):\n",
    "            pdf_writer.addPage(pdf_reader.getPage(page))\n",
    "    # 保存合并后的文件\n",
    "    with open(r\"D:\\电子知识库\\Vescode\\python2.0\", \"wb\") as out:\n",
    "        pdf_writer.write(out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "ERROR: Could not find a version that satisfies the requirement poppler (from versions: none)\n",
      "ERROR: No matching distribution found for poppler\n"
     ]
    }
   ],
   "source": [
    "pip install poppler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D:/电子知识库/语言进化.pdf\n"
     ]
    },
    {
     "ename": "PDFInfoNotInstalledError",
     "evalue": "Unable to get page count. Is poppler installed and in PATH?",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32md:\\2345Downloads\\lib\\site-packages\\pdf2image\\pdf2image.py\u001b[0m in \u001b[0;36mpdfinfo_from_path\u001b[1;34m(pdf_path, userpw, poppler_path, rawdates, timeout)\u001b[0m\n\u001b[0;32m    457\u001b[0m             \u001b[0menv\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"LD_LIBRARY_PATH\"\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpoppler_path\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m\":\"\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0menv\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"LD_LIBRARY_PATH\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 458\u001b[1;33m         \u001b[0mproc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mPopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcommand\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0menv\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstdout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mPIPE\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstderr\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mPIPE\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    459\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\2345Downloads\\lib\\subprocess.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, user, group, extra_groups, encoding, errors, text, umask)\u001b[0m\n\u001b[0;32m    950\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 951\u001b[1;33m             self._execute_child(args, executable, preexec_fn, close_fds,\n\u001b[0m\u001b[0;32m    952\u001b[0m                                 \u001b[0mpass_fds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcwd\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\2345Downloads\\lib\\subprocess.py\u001b[0m in \u001b[0;36m_execute_child\u001b[1;34m(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_gid, unused_gids, unused_uid, unused_umask, unused_start_new_session)\u001b[0m\n\u001b[0;32m   1419\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1420\u001b[1;33m                 hp, ht, pid, tid = _winapi.CreateProcess(executable, args,\n\u001b[0m\u001b[0;32m   1421\u001b[0m                                          \u001b[1;31m# no special security\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [WinError 2] 系统找不到指定的文件。",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mPDFInfoNotInstalledError\u001b[0m                  Traceback (most recent call last)",
      "\u001b[1;32mD:\\temp/ipykernel_11876/12469531.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     20\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     21\u001b[0m \u001b[1;31m# PDF转换为图片\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 22\u001b[1;33m \u001b[0mconvert_to_pic\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr\"D:/电子知识库/语言进化.pdf\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34mr\"D:/电子知识库\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mD:\\temp/ipykernel_11876/12469531.py\u001b[0m in \u001b[0;36mconvert_to_pic\u001b[1;34m(filepath, pic_dirpath)\u001b[0m\n\u001b[0;32m     12\u001b[0m         \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmakedirs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpic_dirpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m     \u001b[0mimages\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mconvert_from_bytes\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilepath\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'rb'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     15\u001b[0m     \u001b[1;31m# images = convert_from_path(filepath, dpi=200)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mimage\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mimages\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\2345Downloads\\lib\\site-packages\\pdf2image\\pdf2image.py\u001b[0m in \u001b[0;36mconvert_from_bytes\u001b[1;34m(pdf_file, dpi, output_folder, first_page, last_page, fmt, jpegopt, thread_count, userpw, use_cropbox, strict, transparent, single_file, output_file, poppler_path, grayscale, size, paths_only, use_pdftocairo, timeout, hide_annotations)\u001b[0m\n\u001b[0;32m    281\u001b[0m             \u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpdf_file\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    282\u001b[0m             \u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 283\u001b[1;33m             return convert_from_path(\n\u001b[0m\u001b[0;32m    284\u001b[0m                 \u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    285\u001b[0m                 \u001b[0mdpi\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdpi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\2345Downloads\\lib\\site-packages\\pdf2image\\pdf2image.py\u001b[0m in \u001b[0;36mconvert_from_path\u001b[1;34m(pdf_path, dpi, output_folder, first_page, last_page, fmt, jpegopt, thread_count, userpw, use_cropbox, strict, transparent, single_file, output_file, poppler_path, grayscale, size, paths_only, use_pdftocairo, timeout, hide_annotations)\u001b[0m\n\u001b[0;32m     96\u001b[0m         \u001b[0mpoppler_path\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpoppler_path\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mas_posix\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     97\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 98\u001b[1;33m     \u001b[0mpage_count\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpdfinfo_from_path\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpdf_path\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0muserpw\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpoppler_path\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mpoppler_path\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"Pages\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     99\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    100\u001b[0m     \u001b[1;31m# We start by getting the output format, the buffer processing function and if we need pdftocairo\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\2345Downloads\\lib\\site-packages\\pdf2image\\pdf2image.py\u001b[0m in \u001b[0;36mpdfinfo_from_path\u001b[1;34m(pdf_path, userpw, poppler_path, rawdates, timeout)\u001b[0m\n\u001b[0;32m    482\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    483\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 484\u001b[1;33m         raise PDFInfoNotInstalledError(\n\u001b[0m\u001b[0;32m    485\u001b[0m             \u001b[1;34m\"Unable to get page count. Is poppler installed and in PATH?\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    486\u001b[0m         )\n",
      "\u001b[1;31mPDFInfoNotInstalledError\u001b[0m: Unable to get page count. Is poppler installed and in PATH?"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from pdf2image import convert_from_path, convert_from_bytes\n",
    "def convert_to_pic(filepath, pic_dirpath):\n",
    "    \"\"\"\n",
    "    每一页的PDF转换成图片\n",
    "    @param filepath:pdf文件路径\n",
    "    @param pic_dirpath:图片目录路径\n",
    "    @return:\n",
    "    \"\"\"\n",
    "    print(filepath)\n",
    "    if not os.path.exists(pic_dirpath):\n",
    "        os.makedirs(pic_dirpath)\n",
    "\n",
    "    images = convert_from_bytes(open(filepath, 'rb').read())\n",
    "    # images = convert_from_path(filepath, dpi=200)\n",
    "    for image in images:\n",
    "        # 保存图片\n",
    "        pic_filepath = os.path.join(pic_dirpath, 'img_'+str(images.index(image))+'.png')\n",
    "        image.save(pic_filepath, 'PNG')\n",
    "        \n",
    "# PDF转换为图片\n",
    "convert_to_pic(r\"D:/电子知识库/语言进化.pdf\",r\"D:/电子知识库\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.7 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "2dbe5485cf12282196b80a8ac362415f1d4830d1445e2229f35f94c570ef032e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
